export function gethhmm(dateTime) {
	return dateTime.getHours() + ':' + dateTime.getMinutes()
}
export function copyObject(o) {
	return JSON.parse(JSON.stringify(o))
}
export function getYMDHMS(d) {
	return (
		d.getFullYear() +
		'/' +
		(d.getMonth() + 1) +
		'/' +
		d.getDate() +
		' ' +
		d.getHours() +
		':' +
		d.getMinutes() +
		':' +
		d.getSeconds()
	)
}
import {
	Solar,
	Lunar,
	LunarUtil,
	Tao,
	SolarUtil
} from 'lunar-javascript'

export class 日历 {
	constructor(aday) {
		this.aday = aday ? aday : new Date()
		this.公历 = Solar.fromDate(aday)

		this.week = `星期${this.公历.getWeekInChinese()}`
		this.公历年 = this.公历.getYear()
		this.公历月 = this.公历.getMonth()
		this.公历日 = this.公历.getDay()
		this.阴历 = Lunar.fromDate(this.aday)
		this.阴历年 = this.阴历.getYearInChinese()
		this.阴历月 = this.阴历.getMonthInChinese()
		this.阴历日 = this.阴历.getDayInChinese()
		this.干支年 = this.阴历.getYearInGanZhi()
		this.干支月 = this.阴历.getMonthInGanZhi()
		this.干支日 = this.阴历.getDayInGanZhi()
		this.生肖 = this.阴历.getYearShengXiao()
		this.宜 = this.阴历.getDayYi()
		this.忌 = this.阴历.getDayJi()
		this.时辰 = LunarUtil.convertTime(gethhmm(aday)) + '时'
		this.道历 = Tao.fromLunar(this.阴历)
		this.戊日 = this.道历.isDayMingWu() ?
			'明戊' :
			this.道历.isDayAnWu() ?
			'暗戊' :
			''
	}
	static getWuInYear(year) {
		// console.log('xxx')
		//{ date: '阳历', lunar: '道历', wuri: '戊日' }
		let table = []
		let days = SolarUtil.getDaysOfYear(year)
		let start = Solar.fromDate(new Date(`${year}`, 0))
		for (let i = 0; i < days; i++) {
			let aday = start.next(i)
			let tao = Tao.fromLunar(aday.getLunar())
			if (tao.isDayWu()) {
				table.push({
					date: aday.getMonth() + '月' + aday.getDay() + '日',
					lunar: tao.getMonthInChinese() + '月' + tao.getDayInChinese() + '日',
					wuri: tao.isDayAnWu() ? '暗戊' : '明戊'
				})
			}
		}
		// console.log(table)
		return table
	}
}
export function getSecond(hour, minutes) {
	return hour * 60 * 60 + minutes * 60
}
export class Timer {
	timerId = null
	time = 0
	state = 'noBegin'
	startCb = []
	endCb = []
	updateCb = []
	startTime = null
	keepAlive = false
	constructor() {}
	start(params) {
		this.startCb.every((func) => {
			func(this.time, this.timerId)
		})
		if (params?.keepAlive) {
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
			this.keepAlive = params.keepAlive
		}
		this.timerId = setInterval(() => {
			this.time += 1
			this.updateCb.every((func) => {
				func(this)
			})
			console.log('内部定时器执行中')
		}, 1000)
		this.startTime = new Date()
		this.state = 'runing'
	}
	end() {
		clearInterval(this.timerId)
		if (this.keepAlive) {
			// 保持屏幕常亮
			uni.setKeepScreenOn({
				keepScreenOn: false
			});
		}
		this.startTime = null
		this.state = 'end'

		this.time = 0
		this.timerId = null
		this.endCb.every((func) => {
			func(this)
		})
	}
	pause() {
		clearInterval(this.timerId)
		this.state = 'pause'
	}
	continue () {
		this.timerId = setInterval(() => {
			this.time += 1
			this.updateCb.every((func) => {
				func(this)
			})
			console.log('内部定时器执行中')
		}, 1000)
		this.state = 'runing'
	}
	getCurrenTime(format) {
		if (format == 'm:s') {
			let s = this.time % 60
			let m = this.time / 60
			return m + ':' + s
		} else {
			return this.time + ''
		}
	}
}
export class LongAlive {
	static getItem(key) {
		return uni.getStorageSync(key)
	}
	static setItem(key, value) {
		uni.setStorageSync(key, value)
	}
	static removeItem(key) {
		uni.removeStorageSync(key)
	}
}

export class Audio {
	context = null
	status = null
	playList = null
	index = null
	currentName = ''
	playType = null
	constructor(playList) {
		this.context = uni.createInnerAudioContext()
		this.context.autoplay = true
		this.playList = playList
		// this.next()
		this.context.src = playList[0].src
		// this.status = "pause"
		this.context.onPlay(() => {
			console.log('开始播放')
		})
		this.context.onEnded((res) => {
			console.log('播放结束')
			this.next()
		})
		this.context.onError((res) => {
			console.log(res.errMsg)
			console.log(res.errCode)
		})
		if (this.playType == '定时播放') {
			//
		}
	}
	next() {
		//根据播放模式生成下一个要播放的音乐
		let name = ''
		if (this.playType === '单曲循环') {
			this.playByName(this.currentName)
		} else if (this.playType === '单曲') {
			//结束
			this.stop()
		} else if (this.playType == '随机播放') {
			let i = Int(this.playList.length * Math.random())
			this.playByName(this.playList[i].name)
			// this.stop()
		} else {
			this.stop()
		}
	}
	stop() {
		this.context.stop()
		this.status = 'stop'
	}
	pause() {
		this.context.pause()
		this.status = 'pause'
	}
	countiue() {
		this.context.play()
		this.status = 'playing'
	}
	destroy() {
		this.context.destroy()
		this.status = null
	}
	getCurrentMusic() {
		return {
			playList: this.playList,
			name: this.currentName
		}
	}
	setPlayList(playList) {
		this.playList = playList
	}
	playByName(name) {
		let music = this.playList.find((item) => item.name === name)
		if (music) {
			if (this.status == 'playing' && this.currentName == name)
				return console.log('正在播放同名音乐')
			if (this.currentName !== this.name) this.context.src = music.src
			this.context.play()
			this.status = 'playing'
			this.currentName = name
		}
	}
	setplayType(type) {
		this.playType = type
	}
}
